package com.ilink.ilinkapi.config;

/**
 * <h3>new-ilink-java-backend-psy</h3>
 *
 * @Description <p>用于配置OpenFeign</p>
 * @Author pengshiyao
 * @Date 2024-10-03 21:59
 **/

import com.ilink.ilinkcommon.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

/**
 * @ClassName DefaultFeignConfig
 * @Description
 * @Author pengshiyao
 * @Date 2024-10-03 21:59
 **/

public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 获取登录用户
                String userId = UserContext.getUserId();
                if(userId == null) {
                    // 如果为空则直接跳过
                    return;
                }
                // 如果不为空则放入请求头中，传递给下游微服务
                template.header("userId", userId);
            }
        };
    }

}
